var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: "",
    pid: "",
    name: "",
    time: "",
    nowtime: "",
    nowData:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    this.CurentTime();
    this.CurentData();
    this.setData({
      pid: option.pid,
      name: option.name
    })
  },
  // 获取当前年月
  CurentData: function () {
    var that = this;
    var now = new Date();
    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日

    var clock = year + "-";

    if (month < 10)
      clock += "0";

    clock += month + "-";

    if (day < 10)
      clock += "0";

    clock += day + " ";
    that.setData({
      nowData: clock
    })
  },
  // 获取当前时间分秒
   CurentTime: function () {
     var that = this;
     var now = new Date();
     var hh = now.getHours();            //时
     var mm = now.getMinutes();          //分
     var ss = now.getSeconds()           //秒
     var clock = "";
    if (hh < 10)
      clock += "0";

    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm + ":";
    if (ss < 10) clock += '0';
    clock += ss;
    that.setData({
      nowtime: clock
    })
  },
  //日期
  bindDateChange: function (e) {
    var date = e.detail.value;
    this.setData({
      date: date
    })
  },
  //bindTimeChange时间
  bindTimeChange: function (e) {
    var time = e.detail.value;
    this.setData({
      time: time
    })
  },
//预约提交
  formSubmit: function (e) {
    this.CurentTime();//获取当前时间
    var that = this;
    if (that.data.date == "" && that.data.time == "") {
      var applytime = that.data.nowData + '\t' + that.data.nowtime;
    }
    else if (that.data.date == "" && that.data.time != "") {
      var applytime = that.data.nowData + '\t' + that.data.time;
    }
    else if (that.data.date != "" && that.data.time == "") {
      var applytime = that.data.date + '\t' + that.data.nowtime;
    }
    else {
      var applytime = that.data.date + '\t' + that.data.time;
    }
  
    var realname = e.detail.value.realname;
    var phone = e.detail.value.phone;
    var remark = e.detail.value.leave;
    var pid = that.data.pid;
    if (pid == "" || pid == undefined) {
      pid = 0
      if (remark == "") {
        wx.showModal({
          title: '提示',
          content: '请输入留言',
          showCancel: false
        })
        return
      } else {
        if (realname == "" || pid == undefined) {
          realname = '小程序'
        }
        if (phone == "" || pid == undefined) {
          phone = '18814450536'
        }
        wx.request({
          url: app.globalData.host + '/api/user/AppointApply',
          method: 'POST',
          data: {
            Uid: app.globalData.userInfo.Uid,
            Pid: pid,
            ApplyTime: applytime,
            RealName: realname,
            Phone: phone,
            Remark: remark
          },
          success: function (res) {
            if (res.data.Code == 501) {
              wx.showToast({
                title: res.data.Message,
                icon: 'false',
                duration: 2000
              })
              return false;
            } else {
              wx.showToast({
                title: '提交成功',
                icon: 'success',
                duration: 2000
              })
            }
          },
          fail: function (error) {
            console.log(error)
          }
        })
      }
    } else {
      if (realname == "" || realname == null) {
        wx.showModal({
          title: '提示',
          content: '请填写预约人',
          showCancel: false
        })
        return
      }
      if (phone == "") {
        wx.showModal({
          title: '提示',
          content: '请填写手机号',
          showCancel: false
        })
        return
      }else {
        if (remark == "") {
          wx.request({
            url: app.globalData.host + '/api/user/AppointApply',
            method: 'POST',
            data: {
              Uid: app.globalData.userInfo.Uid,
              Pid: pid,
              ApplyTime: applytime,
              RealName: realname,
              Phone: phone
            },
            success: function (res) {
              if (res.data.Code == 501) {
                wx.showToast({
                  title: res.data.Message,
                  icon: 'false',
                  duration: 2000
                })
                return false;
              } else {
                wx.showToast({
                  title: '提交成功',
                  icon: 'success',
                  duration: 2000
                })
              }
            },
            fail: function (error) {
              console.log(error)
            }
          })
        } else {
          wx.request({
            url: app.globalData.host + '/api/user/AppointApply',
            method: 'POST',
            data: {
              Uid: app.globalData.userInfo.Uid,
              Pid: pid,
              ApplyTime: applytime,
              RealName: realname,
              Phone: phone,
              Remark: remark
            },
            success: function (res) {
              if (res.data.Code == 501) {
                wx.showToast({
                  title: res.data.Message,
                  icon: 'false',
                  duration: 2000
                })
                return false;
              } else {
                wx.showToast({
                  title: '提交成功',
                  icon: 'success',
                  duration: 2000
                })
              }
            },
            fail: function (error) {
              console.log(error)
            }
          })
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})